import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui';
import axios from 'axios'
import 'element-ui/lib/theme-chalk/index.css';
import 'element-ui/lib/theme-chalk/display.css';

axios.defaults.baseURL = 'http://localhost:7000/'
axios.defaults.timeout = 3000
axios.defaults.headers = { 'Content-type': 'application/json; charset=UTF-8' }

axios.interceptors.request.use(
  (config) => {
    let token = localStorage.getItem("token")
    if (token) {
      config.headers["Authorization"] = token
    }
    if (config.method == 'post' || config.method == 'put') {
      config.data = JSON.stringify(config.data)
    }
    return config
  },
  (err) => {
    return Promise.resolve(err)
  }
)

axios.interceptors.response.use(
  (response) => {
    if (!response || !response.data || !response.data.success) {
      console.log("test")
    }
    if (response) {
      console.log(response)
    }
    return response
  }, (err) => {
    switch (err.response.data.statusCode) {
      case 401:
        alert("请先登录！, 即将跳转")
        setTimeout(() => {
          router.replace({
            path: '/login'
          })
        }, 500)
        break
      case 403:
        setTimeout(() => {
          router.replace({
            path: '/forbidden'
          })
        }, 500)
        break
      case 400:
        setTimeout(() => {
          router.replace({
            path: '/error'
          })
        }, 500)
        break
      case 500:
        setTimeout(() => {
          router.replace({
            path: '/error'
          })
        }, 500)
        break
    }
    return err
  }
)



const axiosHttp = {
  get(url, params = {}) {
    return new Promise((resolve, reject) => {
      axios
        .get(url, { params })
        .then((res) => {
          console.log(res)
          resolve(res.data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  post(url, params = {}) {
    return new Promise((resolve, reject) => {
      axios
        .post(url, params)
        .then((res) => {
          resolve(res.data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  put(url, params = {}) {
    return new Promise((resolve, reject) => {
      axios
        .put(url, params)
        .then((res) => {
          resolve(res.data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  delete(url, params = {}) {
    return new Promise((resolve, reject) => {
      axios
        .delete(url, params)
        .then((res) => {
          resolve(res.data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  }
}


Vue.prototype.$http = axiosHttp;
Vue.config.productionTip = false
Vue.use(ElementUI);
Vue.prototype.$articleLoading = false;

Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "H+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

const utf8to16 = function (str) {
  var out, i, len, c;
  var char2, char3;
  out = "";
  len = str.length;
  i = 0;
  while (i < len) {
    c = str.charCodeAt(i++);
    switch (c >> 4) {
      case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
        // 0xxxxxxx
        out += str.charAt(i - 1);
        break;
      case 12: case 13:
        // 110x xxxx 10xx xxxx
        char2 = str.charCodeAt(i++);
        out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
        break;
      case 14:
        // 1110 xxxx 10xx xxxx 10xx xxxx
        char2 = str.charCodeAt(i++);
        char3 = str.charCodeAt(i++);
        out += String.fromCharCode(((c & 0x0F) << 12) |
          ((char2 & 0x3F) << 6) |
          ((char3 & 0x3F) << 0));
        break;
    }
  }
  return out;
}

Vue.prototype.$utf8to16 = utf8to16;

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
